package de.grosjohann.android.feuerwehr;

import java.util.List;

import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import de.grosjohann.android.feuerwehr.adapter.WeatherListAdapter;
import de.grosjohann.android.feuerwehr.domain.FireIndex;
import de.grosjohann.android.feuerwehr.domain.WeatherWarning;
import de.grosjohann.android.feuerwehr.notification.Notify;
import de.grosjohann.android.feuerwehr.panel.Panel;
import de.grosjohann.android.feuerwehr.service.IntentServiceFacade;
import de.grosjohann.android.feuerwehr.service.WeatherDisplayService;
import de.grosjohann.android.feuerwehr.service.WeatherNotificationService;
import de.grosjohann.android.feuerwehr.util.ColorUtil;
import de.grosjohann.android.feuerwehr.util.JSON2ObjectUtil;

public class WeatherActivity
  extends Activity {

  private static final String NAME = "WeatherActivity";

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.weather);

    ListView lv = (ListView) findViewById(R.id.lvWeatherWarning);
    BaseAdapter baseAdapter = new WeatherListAdapter(this, R.layout.weatherwarning);
    lv.setAdapter(baseAdapter);

    lv.setTextFilterEnabled(false);
    lv.setDividerHeight(10);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
      Log.i(NAME, "started - has extras");
      updateWeatherList((String) extras.get(WeatherNotificationService.WEATHER_DATA));
    }

    WeatherDisplayService.updateUIListener = new IUpdateUIListener() {
      @Override
      public void updateUI(final String newData) {
        runOnUiThread(new Runnable() {
          @Override
          public void run() {
            updateWeatherList(newData);
          }
        });
      }

      @Override
      public void showInfo(String info) {
        // nothing to do
      }

      @Override
      public void showWarning(String warning) {
        // nothing to do
      }

      @Override
      public void showError(final String error) {
        runOnUiThread(new Runnable() {
          @Override
          public void run() {
            Panel panel = new Panel(getApplicationContext(), WeatherActivity.this);
            panel.showError(error);
          }
        });

      }

    };
  }

  @Override
  protected void onResume() {
    super.onResume();
    Notify.getInstance().clear(this);

    IntentServiceFacade.startOnce(this, WeatherDisplayService.class);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.weather, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
      case R.id.miWeatherInfo:
        startActivity(new Intent(this, WeatherInfoActivity.class));
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  /*
   * Update of WeatherData.
   */
  private void updateWeatherList(String newData) {
    try {

      /*
       * Waldbrandindex
       */
      LinearLayout llFireIndex = (LinearLayout) findViewById(R.id.llFireIndex);
      FireIndex fi = JSON2ObjectUtil.getFireIndex(newData);
      llFireIndex.setBackgroundColor(ColorUtil.getAlphaColor(136, fi.getColor()));

      TextView tvFireIndexMarker = (TextView) findViewById(R.id.tvFireIndexMarker);
      tvFireIndexMarker.setBackgroundColor(fi.getColor());

      TextView tvFireIndexMsg = (TextView) findViewById(R.id.tvFireIndexMsg);
      tvFireIndexMsg.setText(fi.getLevel().getResId());

      /*
       * Liste der Wetter-Meldungen
       */

      List<WeatherWarning> list = JSON2ObjectUtil.getWeatherWarningList(newData);

      ListView lv = (ListView) findViewById(R.id.lvWeatherWarning);
      ((WeatherListAdapter) lv.getAdapter()).addAll(list);

    } catch (JSONException ex) {
      Log.e(NAME, ex.getMessage(), ex);

      Panel panel = new Panel(getApplicationContext(), this);
      panel.showError(R.string.errReceiveWeatherData);
    }
  }
}
